//
//  LBMediaPresentViewController.m
//  LangBa
//
//  Created by Mac on 2017/12/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "OEPwdInPutViewController.h"
#import "UITextField+STInputLimit.h"
#import "NSString+STMD5.h"
#define PWD_COUNT 6
@interface OEPwdInPutViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;

@property(nonatomic, strong) NSString                     *message;/**<  */
@property(nonatomic, copy) TTAlertDismissedHandleAction                    dismissedHandleAction;/**<  */
@property(nonatomic, strong) NSArray                     *titlesArray;/**<  */

@property(nonatomic, strong) STLabel                     *alertLable;/**< 提示lable */
@property(nonatomic, strong) UITextField                     *pwdTextFiled;/**<  */
@end

@implementation OEPwdInPutViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
- (instancetype)initWithMessage:(NSString*)message
                     titleArray:(NSArray*)titleArray
          dismissedHandleAction:(TTAlertDismissedHandleAction)dismissedHandleAction{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
        self.message = message;
        self.titlesArray = titleArray;
        self.dismissedHandleAction = dismissedHandleAction;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
    [self addNotifacations];
    DDLogInfo(@"正确密码是:%@",self.pwd);
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(tt_textFiledDidChange:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)tt_textFiledDidChange:(NSNotification*)notif{
    if (notif.object == self.pwdTextFiled) {
        [self.pwdTextFiled st_textInputLimitWithLength:PWD_COUNT];
        if (self.pwdTextFiled.text.length == PWD_COUNT) {
            //判断密码
            NSString * pwd = self.pwdTextFiled.text.st_md532BitLower.st_md532BitLower;
            if ([pwd isEqualToString:self.pwd]) {
                [self dismissWithHandle:^{
                    if (self.onInputTruePwd) {
                        self.onInputTruePwd();
                    }
                }];
            }else{
                //展示错误动画
                [self rightLablebackAnimation];
                
            }
        }
    }
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)rightLablebackAnimation{
    self.alertLable.text = @"Password error, please re - enter 6 - digit password";
    self.alertLable.textColor = FlatRed;
    [UIView animateWithDuration:0.15 animations:^{
        self.alertLable.left = 15 + 40;
    } completion:^(BOOL finished) {
        [self alertLablebackAnimation];
    }];
}
- (void)alertLablebackAnimation{
    [UIView animateWithDuration:0.16 delay:0
         usingSpringWithDamping:0.05 initialSpringVelocity:8 options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.alertLable.left = 15 ;
                     } completion:^(BOOL finished) {
                         
                     }];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        if ([weakSelf.pwdTextFiled isFirstResponder]) {
            [weakSelf onSelectedWhiteView];
        }else{
            [weakSelf dismiss];
        }
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth - 60, 100)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = [UIColor whiteColor];
    self.customView.layer.cornerRadius = 10;
    self.customView.clipsToBounds = YES;
    self.customView.centerY = UIScreenHeight / 2;
    [self.view addSubview:self.customView];
    
    STButton * alertButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, self.customView.width, 40)
                                                       title:@"pay password"
                                                  titleColor:UIColor.blackColor
                                                   titleFont:18
                                                cornerRadius:0
                                             backgroundColor:nil
                                             backgroundImage:[UIImage imageNamed:@"icon_alert_bg"]
                                                       image:nil];
    [self.customView addSubview:alertButton];
    alertButton.userInteractionEnabled = NO;
    alertButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    
    
    STLabel * nameLable = [[STLabel alloc] initWithFrame:CGRectMake(15, alertButton.bottom + 20, self.customView.width - 30 , 40)
                                                    text:@"please input pay password"
                                               textColor:FirstTextColor
                                                    font:13
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentCenter];
    [self.customView addSubview:nameLable];
    self.alertLable = nameLable;
    
    
    self.pwdTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(30, nameLable.bottom, self.customView.width - 60, 30)];
    [self.pwdTextFiled st_showBottomLine];
    self.pwdTextFiled.textAlignment = NSTextAlignmentCenter;
    self.pwdTextFiled.secureTextEntry = YES;
    self.pwdTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    [self.customView addSubview:self.pwdTextFiled];
    [self.pwdTextFiled st_textInputLimitWithLength:4];
    [self st_addAdjustView:self.pwdTextFiled offset:20];
    self.customView.height = self.pwdTextFiled.bottom + 20;
    
    self.customView.center = self.view.center ;
    self.customView.transform = CGAffineTransformMakeScale(0.2, 0.2);
}

#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.transform = CGAffineTransformIdentity;
                     } completion:^(BOOL finished) {
                         [self.pwdTextFiled becomeFirstResponder];
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.transform = CGAffineTransformMakeScale(0.05, 0.05);
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)dismissWithHandle:(void(^)())handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.transform = CGAffineTransformMakeScale(0.05, 0.05);
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}
#pragma mark --Action Method

- (void)onSelectedCancleButton{
    
    [self dismiss];
    
}
- (void)onSelectedWhiteView{
    [self.view endEditing:YES];
}

@end







